import Request from 'utils/request';

export default class WeChartApi {
  static getBindCode() {
    const apiName = 'api/userlinkunionid/getUnionLinkUserCode';

    return Request.post(apiName).then(res => {
      return res.data;
    });
  }

  static getBindUnionid() {
    const apiName = 'api/userlinkunionid/checkBindUnionLinkUser';

    return Request.post(apiName).then(res => {
      return res.data;
    });
  }

  static authLogin(code) {
    const apiName = 'wechat/getCodeFromWechatAuth';

    return Request.post(apiName, {
      code: code
    }).then(res => {
      let data = res.data;
      let weChatUserInfo = data.sessionInfo.weChatUserInfo;
      return {
        authToken: data.auth_token,
        refreshToken: data.refresh_token,
        user: weChatUserInfo
      };
    });
  }

  static bindAccount(bindData, unionid) {
    const apiName = 'api/wehcatshop/bindUnionLinkUserCode';

    return Request.post(apiName, {
      userId: bindData.userId,
      unionid: unionid,
      code: bindData.code
    }, true, true, true).then(res => {
      return res.msg;
    });
  }

  static unbindAccount(user) {
    const apiName = 'api/userlinkunionid/disbind';

    return Request.post(apiName, {
      userId: user.userId,
      unionid: user.unionid,
    }).then(res => {
      return res.msg;
    });
  }
}
